package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    /**
     * 公司地点新增方法
     * @param address
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
//        System.out.println(address);
//        return null;
        try {
            if (address.getAddress()!=null&&!"".equals(address.getAddress())&&address.getLatitude()!=0&&address.getLongitude()!=0){
                addressService.add(address);
                return new Result(true,"添加地址成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"添加地址失败,请检查是否填写正确");
    }

    /**
     * 分页查询以及模糊搜索列表方法
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = addressService.findPage(queryPageBean);
            return new Result(true,"获取列表信息成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取列表信息失败");
        }
    }

    /**
     * 根据id删除地址
     */
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            addressService.delete(id);
            return new Result(true,"删除地址信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除地址信息失败");
        }
    }
}
